package com.itheima.homework.homework02;

import java.util.Comparator;
import java.util.TreeSet;

public class Demo2 {
    /*
       在某次考试中，学生的成绩信息如下： 姓名 年龄 成绩 Tom 20 90 Jerry 22 95 John 20 100 Lily 22 100 Lucy 22 90 Kevin 22 90
       请分别用Comparable和Comparator两个接口对以上同学的成绩做降序排序，
       如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序，成绩和年龄都一样，则按照姓名的字典顺序排序。
     */
    public static void main(String[] args) {
        //TreeSet<Student> set = new TreeSet<>();

        TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = o2.getScore() - o1.getScore();
                int result1 = result == 0 ? (o1.getAge() - o2.getAge()) : result;
                int result2 = result1 == 0 ? o1.getName().compareTo(o2.getName()) : result1;
                return result2;
            }
        });
        Student s1 = new Student("Tom",20,90);
        Student s2 = new Student("Jerry",22,95);
        Student s3 = new Student("John",20,100);
        Student s4 = new Student("Lily",22,100);
        Student s5 = new Student("Lucy",22,90);
        Student s6 = new Student("Kevin",22,90);
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        set.add(s5);
        set.add(s6);

        System.out.println(set);
    }
}
